package com.example.express;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.example.express.base.BaseActivity;


public class WebActivity extends BaseActivity {
    private static String url="";
    private static String title="";
    public static void start(Context context,String url,String title){
        WebActivity.url =url;
        WebActivity.title =title;
        context.startActivity(new Intent(context,WebActivity.class));
    }

    @Override
    protected int initLayout() {
        return R.layout.activity_web;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        WebView webView = findViewById(R.id.web_view);
        TextView textView = findViewById(R.id.text_view);
        TextView ivBack = findViewById(R.id.iv_back);
        textView.setText(title);
        ivBack.setOnClickListener(v -> finish());
        webView.loadUrl(url);
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setSupportZoom(true);
        webSettings.setBuiltInZoomControls(true);
        webSettings.setDisplayZoomControls(false);
        webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        webSettings.setAllowFileAccess(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setDefaultTextEncodingName("utf-8");
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                view.loadUrl(request.getUrl().toString());
                return true;
            }
        });
    }
}